package com.example.wechattakeaway.service.Impl;

import com.example.wechattakeaway.entity.*;
import com.example.wechattakeaway.enums.ResultEnum;
import com.example.wechattakeaway.form.orderform.OrderCommitForm;
import com.example.wechattakeaway.mapper.*;
import com.example.wechattakeaway.service.OrderService;
import com.example.wechattakeaway.utils.ResultVOUtil;
import com.example.wechattakeaway.utils.TimeUtil;
import com.example.wechattakeaway.vo.OrderDetailVo;
import com.example.wechattakeaway.vo.OrderVo;
import com.example.wechattakeaway.vo.ResultVO;
//import com.sun.org.apache.regexp.internal.RE;
import com.github.pagehelper.PageHelper;
import net.sf.saxon.expr.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
@Repository
public class OrderServiceImpl implements OrderService {

    @Autowired
    MerOrderMapper orderMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    MerDishMapper merDishMapper;

    @Autowired
    DishOptionMapper dishOptionMapper;

    @Autowired
    OptionDetailMapper optionDetailMapper;

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    OrderSupplyMapper orderSupplyMapper;

    @Override
    public ResultVO UserCommit(OrderCommitForm orderCommitForm) throws ParseException {
        MerOrder merOrder = new MerOrder();
        merOrder.setUserId(orderCommitForm.getUserId());
        merOrder.setAcceptAddress(orderCommitForm.getAcceptAddress());
        merOrder.setAcceptPhone(orderCommitForm.getAcceptPhone());
        merOrder.setDescription(orderCommitForm.getDescription());
        merOrder.setMerchantId(orderCommitForm.getMerchantId());
        merOrder.setOrderPrice(orderCommitForm.getOrderPrice());

        merOrder.setOrderStatus(1);
        merOrder.setCreateTime(TimeUtil.getNowDateTime());

        try{
            orderMapper.UserCommit(merOrder);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        return ResultVOUtil.success(merOrder);
    }

    @Override
    public ResultVO UserPayment(Integer orderId) {
        try {
            orderMapper.UserPayment(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
    }

    @Override
    public ResultVO MerchantConfirm(Integer orderId) {
        try{
            orderMapper.MerchantConfirm(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO UserConfirm(Integer orderId) {
        try{
            orderMapper.UserConfirm(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO OrderQuit(Integer orderId) {
        try {
            orderMapper.OrderQuit(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO SelectOrderById(Integer orderId) {
        MerOrder order = new MerOrder();
        try{
            order = orderMapper.SelectOrderById(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }

        OrderVo orderVo = new OrderVo();
        orderVo.setAcceptAddress(order.getAcceptAddress());
        orderVo.setAcceptPhone(order.getAcceptPhone());
        orderVo.setCreateTime(order.getCreateTime());
        orderVo.setDescription(order.getDescription());
        orderVo.setMerchantId(order.getMerchantId());
        orderVo.setMerchantName(merchantMapper.selectByPrimaryKey(order.getMerchantId()).getMerchantName());
        orderVo.setOrderId(order.getOrderId());
        orderVo.setOrderPrice(order.getOrderPrice());
        orderVo.setOrderStatus(order.getOrderStatus());
        orderVo.setUserId(order.getUserId());

        //订单详情列表
        List<OrderDetail> orderDetailList = orderDetailMapper.SelectOrderDetailByOrder(orderId);
        //订单详情封装返回列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        //订单补充封装列表
        List<OrderSupply> orderSupplyList = new ArrayList<>();

        for (OrderDetail orderDetail : orderDetailList) {
            OrderDetailVo orderDetailVo = new OrderDetailVo();

            //设置菜品名字
            MerDish merDish = merDishMapper.selectByPrimaryKey(orderDetail.getDishId());
            orderDetailVo.setDishName(merDish.getDishName());
            //设置菜品照片
            orderDetailVo.setDishPhoto(merDish.getDishPhoto());
            //设置菜品购买数量
            orderDetailVo.setDishNum(orderDetail.getDishNum());
            //是否有可选项
            orderDetailVo.setIsChoice(merDish.getIsChoice());
            if (orderDetailVo.getIsChoice() == 1){
                orderSupplyList = orderSupplyMapper.SelectAllByOrder(orderDetail.getOrderDetailId());
                orderDetailVo.setOptionDetail(orderSupplyList);
            }

            orderDetailVoList.add(orderDetailVo);
        }

        orderVo.setOrderDetailVoList(orderDetailVoList);
        return ResultVOUtil.success(orderVo);
    }

    @Override
    public ResultVO SelectOrderByUser(Integer pageNum,Integer userId) {
        PageHelper.startPage(pageNum,5);

        List<MerOrder> merOrderList = new ArrayList<>();
        try{
            merOrderList = orderMapper.SelectOrderByUser(userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(merOrderList);
    }

    @Override
    public ResultVO SelectOrderByMerchant(Integer pageNum,Integer merchantId) {
        PageHelper.startPage(pageNum,5);

        List<MerOrder> merOrderList = new ArrayList<>();
        try{
            merOrderList = orderMapper.SelectOrderByMerchant(merchantId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success(merOrderList);
    }

    @Override
    public ResultVO DeleteOrderById(Integer orderId) {
        try{
            orderMapper.DeleteOrderById(orderId);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVOUtil.error(ResultEnum.NORMAL_ERROR);
        }
        return ResultVOUtil.success();
    }

}
